﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Locator.Silverlight.Client.Models.InventoryServiceReference;
using Locator.Silverlight.Client.Models.ViewModels.Screens;
using Locator.Silverlight.Client.Models;
using System.Collections.ObjectModel;

namespace Locator.Silverlight.Client.Test
{
    [TestClass]
    public class MainViewModelTests
    {
        private static MockInventoryService InitializeMockInventoryService()
        {
            MockInventoryService newMockInventoryService = new MockInventoryService();
            newMockInventoryService.GetProductsResult = new ObservableCollection<ProductListItem>
                            {
                                new ProductListItem(){ Id = 1, Name = "Product 1" }
                            };

            newMockInventoryService.GetStoresResult = new ObservableCollection<Store>
                            {
                                new Store(){ Id = 1, Address = "Store 1", City = "Toronto" }
                            };

            newMockInventoryService.GetInventoriesResult = new ObservableCollection<StoreWithInventory>
                            {
                                new StoreWithInventory()
                                {
                                    Store = new InvStore()
                                    {
                                        StoreNumber=1,
                                        Address="Store 1",
                                        City="Toronto",
                                        Latitude = -79.0M,
                                        Longitude = 41.0M
                                    },
            
                                    Inventory = new ObservableCollection<InventoryInfo>
                                    {
                                        new InventoryInfo()
                                         {
                                             NumberInStock = 5,
                                             Product = new ProductListItem()
                                             {
                                                 Id = 1,
                                                 Name = "Product 1"
                                             }
                                         }
                                    }
                                }
                            };

            return newMockInventoryService;
        }

        [TestMethod]
        public void MainViewModelInitTest()
        {
            MockInventoryService newMockInventoryService = InitializeMockInventoryService();

            using (MainViewModel mainViewModel = new MainViewModel(new InventoryModel(newMockInventoryService)))
            {
                Assert.AreEqual(2, mainViewModel.Products.Count);
                Assert.AreEqual(-1, mainViewModel.Products[0].Id);
                Assert.AreEqual("Your products", mainViewModel.Products[0].Name);
                Assert.AreEqual(1, mainViewModel.Products[1].Id);
                Assert.AreEqual("Product 1", mainViewModel.Products[1].Name);
                Assert.AreEqual(-1, mainViewModel.CurrentProduct.Id);

                Assert.AreEqual(3, mainViewModel.Stores.Count);
                Assert.AreEqual(-1, mainViewModel.Stores[0].Id);
                Assert.AreEqual("Your stores", mainViewModel.Stores[0].Address);
                Assert.AreEqual(-2, mainViewModel.Stores[1].Id);
                Assert.AreEqual("All stores", mainViewModel.Stores[1].Address);
                Assert.AreEqual(1, mainViewModel.Stores[2].Id);
                Assert.AreEqual("Store 1, Toronto", mainViewModel.Stores[2].Address);
                Assert.AreEqual(-1, mainViewModel.CurrentStore.Id);
            }
        }

        [TestMethod]
        public void MainViewModelSearchInventoriesTest()
        {
            MockInventoryService newMockInventoryService = InitializeMockInventoryService();

            using (MainViewModel mainViewModel = new MainViewModel(new InventoryModel(newMockInventoryService)))
            {
                mainViewModel.FindProductsCommand.Execute(null);
                Assert.AreEqual(1, mainViewModel.StoresWithInventories.Count);
                Assert.AreEqual(-79.0, mainViewModel.StoresWithInventories[0].Location.Latitude);
                Assert.AreEqual(41.0, mainViewModel.StoresWithInventories[0].Location.Longitude);
                Assert.AreEqual("Store 1 Toronto", mainViewModel.StoresWithInventories[0].Address);
                Assert.AreEqual(1, mainViewModel.StoresWithInventories[0].InventoryInfo.Count);
                Assert.AreEqual(5, mainViewModel.StoresWithInventories[0].InventoryInfo[0].NumberInStock);
                Assert.AreEqual(1, mainViewModel.StoresWithInventories[0].InventoryInfo[0].Product.Id);
                Assert.AreEqual("Product 1", mainViewModel.StoresWithInventories[0].InventoryInfo[0].Product.Name);
            }
        }
    }
}